package com.tomecode.tools.maven.settings.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

import com.tomecode.util.gui.Dialog;
import com.tomecode.util.gui.HideNotifiListener;
import com.tomecode.util.gui.PanelFactory;
import com.tomecode.util.gui.component.Button;

/**
 * Frm list of plugin groups in local repository
 * 
 * @author Frastia Tomas
 * 
 */
public final class FrmListOfPluginGroups extends Dialog {

	private static final long serialVersionUID = -691686530375650577L;

	private final JList listPluginGourps;

	private final Button bSelect;

	/**
	 * Construktor
	 * 
	 * @param profiles
	 */
	private FrmListOfPluginGroups(final List<String> selectedPluginGroups, final List<String> allPluginGroups ) {
		super(null, "List of Plugin Groups in repository", 400, 350, false, true, true, true);

		listPluginGourps = new JList(allPluginGroups.toArray());
		listPluginGourps.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		listPluginGourps.setCellRenderer(new SelectedPluginGroupsRenderer(selectedPluginGroups));
		listPluginGourps.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				selectInList(selectedPluginGroups, allPluginGroups);
			}
		});
		listPluginGourps.addMouseListener(new MouseAdapter() {
			public final void mouseClicked(MouseEvent e) {
				selectInList(selectedPluginGroups, allPluginGroups);
			}
		});
		bSelect = new Button("Select", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				if (listPluginGourps.getSelectedIndex() == -1) {
					return;
				}

				for (Integer i : listPluginGourps.getSelectedIndices()) {
					sendReturnObj(allPluginGroups.get(i));
				}
				hideMe(null);
			}
		});
		addToButtonLayout(bSelect);

		panelRoot.add(PanelFactory.wrapWithTile("Plugin Groups", new JScrollPane(listPluginGourps)), BorderLayout.CENTER);
	}

	private final void selectInList(List<String> selectedPluginGroups, List<String> allPluginGroups) {
		if (listPluginGourps.getSelectedIndex() == -1) {
			bSelect.setEnabled(false);
			return;
		}
		int index = listPluginGourps.getSelectedIndex();
		bSelect.setEnabled(!selectedPluginGroups.contains(allPluginGroups.get(index)));
	}

	public static final void showMe( final List<String> selectedPluginGroups, final List<String> allPluginGroups , final HideNotifiListener notifiListener) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new FrmListOfPluginGroups(selectedPluginGroups, allPluginGroups).addHideListeners(notifiListener).setVisible(true);
			}
		});
	}

	/**
	 * 
	 * selected profile renderer
	 * 
	 * @author Frastia Tomas
	 * 
	 */
	private final class SelectedPluginGroupsRenderer implements ListCellRenderer {
		protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

		private final List<String> selectedPluginGroups;

		private SelectedPluginGroupsRenderer(List<String> selectedPluginGroups) {
			this.selectedPluginGroups = selectedPluginGroups;
		}

		public final Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
			final Component c = defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
			if (selectedPluginGroups.contains(value.toString())) {
				c.setForeground(Color.gray);
			} else {
				c.setForeground(Color.black);
			}
			return c;
		}
	}

}
